﻿#include "GuildDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../MirSounds/SoundManager.h"
#include "../../Settings.h"
#include "../GameScene.h"
#include "../../MirControls/MirMessageBox.h"
#include "../../Forms/Client.CMain.h"
#include "../../../Shared/ClientPackets.h"
#include "../../MirNetwork/Network.h"
#include "../../MirObjects/MapObject.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirControls/MirAmountBox.h"
#include "../../../Shared/Language.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirObjects;
using namespace Client::MirSounds;
using namespace SlimDX::Direct3D9;
namespace S = ServerPackets;
namespace C = ClientPackets;
using Effect = Client::MirObjects::Effect;
using namespace Client::MirScenes::Dialogs;

namespace Client::MirScenes::Dialogs
{

int GuildDialog::MyRankId = 0;
GuildRankOptions GuildDialog::MyOptions = static_cast<GuildRankOptions>(0);

    GuildRankOptions GuildDialog::GetMyOptions()
    {
        return MyOptions;
    }

    GuildDialog::GuildDialog()
    {
        setIndex(180);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        setSort(true);
        setLocation(getCenter());

        BeforeDraw += [&] (o, e)
        {
            RefreshInterface();
        };

        #pragma region TabUI

        TitleLabel = new MirImageControl();
        TitleLabel->setIndex(25);
        TitleLabel->setLibrary(Libraries::Title);
        Point tempVar(18, 9);
        TitleLabel->setLocation(&tempVar);
        TitleLabel->setParent(this);

        NoticeButton = new MirButton();
        NoticeButton->setLibrary(Libraries::Title);
        NoticeButton->setIndex(93);
        NoticeButton->setPressedIndex(94);
        NoticeButton->setSound(SoundList::ButtonA);
        NoticeButton->setParent(this);
        Point tempVar2(20, 38);
        NoticeButton->setLocation(&tempVar2);
        NoticeButton->Click += [&] (o, e)
        {
            LeftDialog(0);
        };
        MembersButton = new MirButton();
        MembersButton->setLibrary(Libraries::Title);
        MembersButton->setIndex(99);
        MembersButton->setPressedIndex(100);
        MembersButton->setSound(SoundList::ButtonA);
        MembersButton->setParent(this);
        Point tempVar3(91, 38);
        MembersButton->setLocation(&tempVar3);
        MembersButton->Click += [&] (o, e)
        {
            LeftDialog(1);
        };
        StorageButton = new MirButton();
        StorageButton->setLibrary(Libraries::Title);
        StorageButton->setIndex(105);
        StorageButton->setPressedIndex(106);
        StorageButton->setSound(SoundList::ButtonA);
        StorageButton->setParent(this);
        Point tempVar4(162, 38);
        StorageButton->setLocation(&tempVar4);
        StorageButton->setVisible(false);
        StorageButton->Click += [&] (o, e)
        {
            LeftDialog(2);
        };
        RankButton = new MirButton();
        RankButton->setLibrary(Libraries::Title);
        RankButton->setIndex(101);
        RankButton->setSound(SoundList::ButtonA);
        RankButton->setParent(this);
        Point tempVar5(233, 38);
        RankButton->setLocation(&tempVar5);
        RankButton->setVisible(false);
        RankButton->Click += [&] (o, e)
        {
            LeftDialog(3);
        };

        StatusButton = new MirButton();
        StatusButton->setLibrary(Libraries::Title);
        StatusButton->setParent(this);
        StatusButton->setIndex(103);
        Point tempVar6(501, 38);
        StatusButton->setLocation(&tempVar6);
        StatusButton->setSound(SoundList::ButtonA);
        StatusButton->Click += [&] (o, e)
        {
            RightDialog(0);
        };

        BuffButton = new MirButton();
        BuffButton->setLibrary(Libraries::Title);
        BuffButton->setParent(this);
        BuffButton->setIndex(95);
        Point tempVar7(430, 38);
        BuffButton->setLocation(&tempVar7);
        BuffButton->setSound(SoundList::ButtonA);
        BuffButton->setVisible(false);
        BuffButton->Click += [&] (o, e)
        {
            RightDialog(1);
        };

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar8(565, 4);
        CloseButton->setLocation(&tempVar8);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Hide();
        };
        #pragma endregion

        #pragma region NoticePageUI
        NoticePage = new MirImageControl();
        NoticePage->setParent(this);
        Size tempVar9(352, 372);
        NoticePage->setSize(&tempVar9);
        Point tempVar10(0, 60);
        NoticePage->setLocation(&tempVar10);
        NoticePage->setVisible(true);
        Notice = new MirTextBox();
        Notice->setForeColour(Color::White);
        System::Drawing::Font tempVar11(Settings::FontName, 8.0F);
        Notice->setFont(&tempVar11);
        Notice->setEnabled(false);
        Notice->setVisible(true);
        Notice->setParent(NoticePage);
        Size tempVar12(322, 330);
        Notice->setSize(&tempVar12);
        Point tempVar13(13, 1);
        Notice->setLocation(&tempVar13);
        Notice->MultiLine();

        NoticeEditButton = new MirButton();
        NoticeEditButton->setVisible(false);
        NoticeEditButton->setIndex(560);
        NoticeEditButton->setHoverIndex(561);
        NoticeEditButton->setPressedIndex(562);
        NoticeEditButton->setLibrary(Libraries::Prguse);
        NoticeEditButton->setSound(SoundList::ButtonA);
        NoticeEditButton->setParent(NoticePage);
        Point tempVar14(20, 342);
        NoticeEditButton->setLocation(&tempVar14);
        NoticeEditButton->Click += [&] (o, e)
        {
            EditNotice();
        };

        NoticeSaveButton = new MirButton();
        NoticeSaveButton->setVisible(false);
        NoticeSaveButton->setIndex(554);
        NoticeSaveButton->setHoverIndex(555);
        NoticeSaveButton->setPressedIndex(556);
        NoticeSaveButton->setLibrary(Libraries::Prguse);
        NoticeSaveButton->setSound(SoundList::ButtonA);
        NoticeSaveButton->setParent(NoticePage);
        Point tempVar15(20, 342);
        NoticeSaveButton->setLocation(&tempVar15);
        NoticeSaveButton->Click += [&] (o, e)
        {
            EditNotice();
        };

        NoticeUpButton = new MirButton();
        NoticeUpButton->setHoverIndex(198);
        NoticeUpButton->setIndex(197);
        NoticeUpButton->setVisible(true);
        NoticeUpButton->setLibrary(Libraries::Prguse2);
        Point tempVar16(337, 1);
        NoticeUpButton->setLocation(&tempVar16);
        Size tempVar17(16, 14);
        NoticeUpButton->setSize(&tempVar17);
        NoticeUpButton->setParent(NoticePage);
        NoticeUpButton->setPressedIndex(199);
        NoticeUpButton->setSound(SoundList::ButtonA);
        NoticeUpButton->Click += [&] (o, e)
        {
            if (NoticeScrollIndex == 0)
            {
                return;
            }
            NoticeScrollIndex--;
            UpdateNotice();
            UpdateNoticeScrollPosition();
        };

        NoticeDownButton = new MirButton();
        NoticeDownButton->setHoverIndex(208);
        NoticeDownButton->setIndex(207);
        NoticeDownButton->setVisible(true);
        NoticeDownButton->setLibrary(Libraries::Prguse2);
        Point tempVar18(337, 318);
        NoticeDownButton->setLocation(&tempVar18);
        Size tempVar19(16, 14);
        NoticeDownButton->setSize(&tempVar19);
        NoticeDownButton->setParent(NoticePage);
        NoticeDownButton->setPressedIndex(209);
        NoticeDownButton->setSound(SoundList::ButtonA);
        NoticeDownButton->Click += [&] (o, e)
        {
            if (NoticeScrollIndex == Notice->getMultiText().size() - 1)
            {
                return;
            }
            if (NoticeScrollIndex >= Notice->getMultiText().size() - 25)
            {
                NoticeScrollIndex--;
            }
            NoticeScrollIndex++;
            UpdateNotice(true);
            UpdateNoticeScrollPosition();
        };

        NoticePositionBar = new MirButton();
        NoticePositionBar->setIndex(206);
        NoticePositionBar->setLibrary(Libraries::Prguse2);
        Point tempVar20(337, 16);
        NoticePositionBar->setLocation(&tempVar20);
        NoticePositionBar->setParent(NoticePage);
        NoticePositionBar->setMovable(true);
        NoticePositionBar->setVisible(true);
        NoticePositionBar->setSound(SoundList::None);
        NoticePositionBar->OnMoving->addListener("NoticePositionBar_OnMoving", std::bind(&GuildDialog::NoticePositionBar_OnMoving, this, std::placeholders::_1, std::placeholders::_2));

        NoticePage->KeyDown += NoticePanel_KeyDown;
        NoticePage->MouseWheel += NoticePanel_MouseWheel;
        #pragma endregion

        #pragma region MembersPageUI
        MembersPage = new MirImageControl();
        MembersPage->setParent(this);
        Size tempVar21(352, 372);
        MembersPage->setSize(&tempVar21);
        Point tempVar22(0, 60);
        MembersPage->setLocation(&tempVar22);
        MembersPage->setVisible(false);
        MembersPageBase = new MirImageControl();
        MembersPageBase->setLibrary(Libraries::Prguse);
        MembersPageBase->setIndex(1852);
        MembersPageBase->setParent(MembersPage);
        Point tempVar23(13, 1);
        MembersPageBase->setLocation(&tempVar23);
        MembersPageBase->setVisible(true);
        MembersPageBase->MouseWheel += MembersPanel_MouseWheel;
        MembersPage->BeforeDraw += [&] (o, e)
        {
            RequestUpdateMembers();
        };
        MembersRanks = std::vector<MirDropDownBox*>(MemberPageRows);
        MembersName = std::vector<MirLabel*>(MemberPageRows);
        MembersStatus = std::vector<MirLabel*>(MemberPageRows);
        MembersDelete = std::vector<MirButton*>(MemberPageRows);

        for (int i = MembersRanks.size() - 1; i >= 0; i--)
        {
            int index = i;
            MirDropDownBox *tempVar24 = new MirDropDownBox();
            tempVar24->setBackColour(i % 2 == 0 ? Color::FromArgb(255, 10, 10, 10) : Color::FromArgb(255, 15, 15, 15));
            tempVar24->setForeColour(Color::White);
            tempVar24->setParent(MembersPage);
            Size tempVar25(100, 14);
            tempVar24->setSize(&tempVar25);
            Point tempVar26(24, 30 + (i * 15));
            tempVar24->setLocation(&tempVar26);
            tempVar24->setVisible(false);
            tempVar24->setEnabled(false);
            MembersRanks[i] = tempVar24;
            MembersRanks[index]->ValueChanged->addListener([&] (o, e)
            {
                OnNewRank(index, MembersRanks[index]->_WantedIndex);
            });
            MembersRanks[index]->MouseWheel += MembersPanel_MouseWheel;

            delete tempVar24;
        }
        for (int i = 0; i < MembersName.size(); i++)
        {
            MirLabel *tempVar27 = new MirLabel();
            tempVar27->setBackColour(i % 2 == 0 ? Color::FromArgb(255, 05, 05, 05) : Color::FromArgb(255, 07, 07, 07));
            tempVar27->setForeColour(Color::White);
            tempVar27->setParent(MembersPage);
            Size tempVar28(100, 14);
            tempVar27->setSize(&tempVar28);
            Point tempVar29(125, 30 + (i * 15));
            tempVar27->setLocation(&tempVar29);
            tempVar27->setVisible(false);
            tempVar27->setEnabled(false);
            System::Drawing::Font tempVar30(Settings::FontName, 7.0F);
            tempVar27->setFont(&tempVar30);
            MembersName[i] = tempVar27;
            MembersName[i]->MouseWheel += MembersPanel_MouseWheel;

            delete tempVar27;
        }
        for (int i = 0; i < MembersStatus.size(); i++)
        {
            MirLabel *tempVar31 = new MirLabel();
            tempVar31->setBackColour(i % 2 == 0 ? Color::FromArgb(255, 10, 10, 10) : Color::FromArgb(255, 15, 15, 15));
            tempVar31->setForeColour(Color::White);
            tempVar31->setParent(MembersPage);
            Size tempVar32(100, 14);
            tempVar31->setSize(&tempVar32);
            Point tempVar33(225, 30 + (i * 15));
            tempVar31->setLocation(&tempVar33);
            tempVar31->setVisible(false);
            tempVar31->setEnabled(false);
            System::Drawing::Font tempVar34(Settings::FontName, 7.0F);
            tempVar31->setFont(&tempVar34);
            MembersStatus[i] = tempVar31;
            MembersStatus[i]->MouseWheel += MembersPanel_MouseWheel;

            delete tempVar31;
        }
        for (int i = 0; i < MembersDelete.size(); i++)
        {
            int index = i;
            MirButton *tempVar35 = new MirButton();
            tempVar35->setEnabled(true);
            tempVar35->setVisible(true);
            Point tempVar36(210, 30 + (i * 15));
            tempVar35->setLocation(&tempVar36);
            tempVar35->setLibrary(Libraries::Prguse);
            tempVar35->setIndex(917);
            tempVar35->setParent(MembersPage);
            MembersDelete[i] = tempVar35;
            MembersDelete[index]->MouseWheel += MembersPanel_MouseWheel;
            MembersDelete[index]->Click += [&] (o, e)
            {
                DeleteMember(index);
            };

            delete tempVar35;
        }
        MembersUpButton = new MirButton();
        MembersUpButton->setHoverIndex(198);
        MembersUpButton->setIndex(197);
        MembersUpButton->setVisible(true);
        MembersUpButton->setLibrary(Libraries::Prguse2);
        Point tempVar37(337, 1);
        MembersUpButton->setLocation(&tempVar37);
        Size tempVar38(16, 14);
        MembersUpButton->setSize(&tempVar38);
        MembersUpButton->setParent(MembersPage);
        MembersUpButton->setPressedIndex(199);
        MembersUpButton->setSound(SoundList::ButtonA);
        MembersUpButton->Click += [&] (o, e)
        {
            if (MemberScrollIndex == 0)
            {
                return;
            }
            MemberScrollIndex--;
            UpdateMembers();
            UpdateMembersScrollPosition();
        };
        MembersDownButton = new MirButton();
        MembersDownButton->setHoverIndex(208);
        MembersDownButton->setIndex(207);
        MembersDownButton->setVisible(true);
        MembersDownButton->setLibrary(Libraries::Prguse2);
        Point tempVar39(337, 318);
        MembersDownButton->setLocation(&tempVar39);
        Size tempVar40(16, 14);
        MembersDownButton->setSize(&tempVar40);
        MembersDownButton->setParent(MembersPage);
        MembersDownButton->setPressedIndex(209);
        MembersDownButton->setSound(SoundList::ButtonA);
        MembersDownButton->Click += [&] (o, e)
        {
            if (MemberScrollIndex == MembersShowCount - MemberPageRows)
            {
                return;
            }
            MemberScrollIndex++;
            UpdateMembers();
            UpdateMembersScrollPosition();
        };

        MembersPositionBar = new MirButton();
        MembersPositionBar->setIndex(206);
        MembersPositionBar->setLibrary(Libraries::Prguse2);
        Point tempVar41(337, 16);
        MembersPositionBar->setLocation(&tempVar41);
        MembersPositionBar->setParent(MembersPage);
        MembersPositionBar->setMovable(true);
        MembersPositionBar->setSound(SoundList::None);
        MembersPositionBar->OnMoving->addListener("MembersPositionBar_OnMoving", std::bind(&GuildDialog::MembersPositionBar_OnMoving, this, std::placeholders::_1, std::placeholders::_2));

        MembersShowOfflineButton = new MirButton();
        MembersShowOfflineButton->setVisible(true);
        MembersShowOfflineButton->setIndex(1346);
        MembersShowOfflineButton->setLibrary(Libraries::Prguse);
        MembersShowOfflineButton->setSound(SoundList::ButtonA);
        MembersShowOfflineButton->setParent(MembersPage);
        Point tempVar42(230, 310);
        MembersShowOfflineButton->setLocation(&tempVar42);
        MembersShowOfflineButton->Click += [&] (o, e)
        {
            MembersShowOfflineSwitch();
        };

        MembersShowOfflineStatus = new MirImageControl();
        MembersShowOfflineStatus->setVisible(true);
        MembersShowOfflineStatus->setIndex(1347);
        MembersShowOfflineStatus->setLibrary(Libraries::Prguse);
        MembersShowOfflineStatus->setParent(MembersPage);
        Point tempVar43(230, 310);
        MembersShowOfflineStatus->setLocation(&tempVar43);
        MembersShowOfflineStatus->Click += [&] (o, e)
        {
            MembersShowOfflineSwitch();
        };

        MembersShowOffline = new MirLabel();
        MembersShowOffline->setVisible(true);
        MembersShowOffline->setText("Show Offline");
        Point tempVar44(245, 309);
        MembersShowOffline->setLocation(&tempVar44);
        MembersShowOffline->setParent(MembersPage);
        Size tempVar45(150, 12);
        MembersShowOffline->setSize(&tempVar45);
        System::Drawing::Font tempVar46(Settings::FontName, 7.0F);
        MembersShowOffline->setFont(&tempVar46);
        MembersShowOffline->setForeColour(Color::White);
        MembersPage->KeyDown += MembersPanel_KeyDown;
        MembersPage->MouseWheel += MembersPanel_MouseWheel;

        #pragma endregion

        #pragma region StatusDialogUI 
        StatusPage = new MirImageControl();
        StatusPage->setParent(this);
        Size tempVar47(230, 372);
        StatusPage->setSize(&tempVar47);
        Point tempVar48(355, 60);
        StatusPage->setLocation(&tempVar48);
        StatusPage->setVisible(true);
        StatusPageBase = new MirImageControl();
        StatusPageBase->setParent(StatusPage);
        StatusPageBase->setLibrary(Libraries::Prguse);
        StatusPageBase->setIndex(1850);
        StatusPageBase->setVisible(true);
        Point tempVar49(10, 2);
        StatusPageBase->setLocation(&tempVar49);
        StatusPage->BeforeDraw += [&] (o, e)
        {
            if (MapControl::getUser()->GuildName == "")
            {
                StatusGuildName->setText("");
                StatusLevel->setText("");
                StatusMembers->setText("");
            }
            else
            {
                StatusGuildName->setText(StringHelper::formatSimple("{0}", MapObject::User->GuildName));
                StatusLevel->setText(StringHelper::formatSimple("{0}", Level));
                StatusMembers->setText(StringHelper::formatSimple("{0}{1}", MemberCount, MaxMembers == 0 ? "" : ("/" + std::to_string(MaxMembers))));
            }
        };
        StatusHeaders = new MirLabel();
        Point tempVar50(7, 47);
        StatusHeaders->setLocation(&tempVar50);
        StatusHeaders->setDrawFormat(TextFormatFlags::Right);
        Size tempVar51(75, 300);
        StatusHeaders->setSize(&tempVar51);
        StatusHeaders->setNotControl(true);
        StatusHeaders->setText("Guild Name\n\nLevel\n\nMembers");
        StatusHeaders->setVisible(true);
        StatusHeaders->setParent(StatusPage);
        StatusHeaders->setForeColour(Color::Gray);
        StatusGuildName = new MirLabel();
        Point tempVar52(82, 47);
        StatusGuildName->setLocation(&tempVar52);
        Size tempVar53(120, 200);
        StatusGuildName->setSize(&tempVar53);
        StatusGuildName->setNotControl(true);
        StatusGuildName->setText("");
        StatusGuildName->setVisible(true);
        StatusGuildName->setParent(StatusPage);
        StatusLevel = new MirLabel();
        Point tempVar54(82, 73);
        StatusLevel->setLocation(&tempVar54);
        Size tempVar55(120, 200);
        StatusLevel->setSize(&tempVar55);
        StatusLevel->setNotControl(true);
        StatusLevel->setText("");
        StatusLevel->setVisible(true);
        StatusLevel->setParent(StatusPage);
        StatusMembers = new MirLabel();
        Point tempVar56(82, 99);
        StatusMembers->setLocation(&tempVar56);
        Size tempVar57(120, 200);
        StatusMembers->setSize(&tempVar57);
        StatusMembers->setNotControl(true);
        StatusMembers->setText("");
        StatusMembers->setVisible(true);
        StatusMembers->setParent(StatusPage);
        StatusExpBar = new MirImageControl();
        StatusExpBar->setVisible(true);
        StatusExpBar->setIndex(423);
        StatusExpBar->setLibrary(Libraries::Prguse2);
        Point tempVar58(322, 403);
        StatusExpBar->setLocation(&tempVar58);
        StatusExpBar->setDrawImage(false);
        StatusExpBar->setNotControl(true);
        StatusExpBar->setParent(this);
        Size tempVar59(260, 15);
        StatusExpBar->setSize(&tempVar59);
        StatusExpBar->BeforeDraw += StatusExpBar_BeforeDraw;
        StatusExpLabel = new MirLabel();
        StatusExpLabel->setVisible(true);
        StatusExpLabel->setDrawFormat(TextFormatFlags::VerticalCenter | TextFormatFlags::HorizontalCenter);
        Point tempVar60(322, 405);
        StatusExpLabel->setLocation(&tempVar60);
        StatusExpLabel->setNotControl(true);
        StatusExpLabel->setParent(this);
        Size tempVar61(260, 15);
        StatusExpLabel->setSize(&tempVar61);
        MembersRecruitName = new MirTextBox();
        Point tempVar62(40, 300);
        MembersRecruitName->setLocation(&tempVar62);
        Size tempVar63(130, 21);
        MembersRecruitName->setSize(&tempVar63);
        MembersRecruitName->setMaxLength(20);
        MembersRecruitName->setParent(StatusPage);
        MembersRecruitName->setVisible(true);
        MembersRecruitName->setText("");
        MembersRecruitName->setBackColour(Color::FromArgb(255, 25, 25, 25));
        MembersRecruitName->setBorder(true);
        MembersRecruitName->setBorderColour(Color::FromArgb(255, 35, 35, 35));
        MembersRecruitName->CanLoseFocus = true;
        RecruitMemberButton = new MirButton();
        RecruitMemberButton->setParent(StatusPage);
        RecruitMemberButton->setEnabled(true);
        RecruitMemberButton->setVisible(true);
        Point tempVar64(170, 298);
        RecruitMemberButton->setLocation(&tempVar64);
        RecruitMemberButton->setLibrary(Libraries::Title);
        RecruitMemberButton->setIndex(356);
        RecruitMemberButton->setHoverIndex(357);
        RecruitMemberButton->setPressedIndex(358);
        RecruitMemberButton->Click += [&] (o, e)
        {
            AddMember();
        };

        RecruitMemberLabel = new MirLabel();
        RecruitMemberLabel->setVisible(true);
        Point tempVar65(36, 283);
        RecruitMemberLabel->setLocation(&tempVar65);
        RecruitMemberLabel->setNotControl(true);
        RecruitMemberLabel->setParent(StatusPage);
        RecruitMemberLabel->setText("Recruit Member");
        Size tempVar66(150, 15);
        RecruitMemberLabel->setSize(&tempVar66);

        #pragma endregion

        #pragma region StorageDialogUI 
        StoragePage = new MirImageControl();
        StoragePage->setParent(this);
        Size tempVar67(352, 372);
        StoragePage->setSize(&tempVar67);
        Point tempVar68(0, 60);
        StoragePage->setLocation(&tempVar68);
        StoragePage->setVisible(false);
        StoragePageBase = new MirImageControl();
        StoragePageBase->setVisible(true);
        StoragePageBase->setParent(StoragePage);
        StoragePageBase->setLibrary(Libraries::Prguse);
        StoragePageBase->setIndex(1851);
        Point tempVar69(30, 19);
        StoragePageBase->setLocation(&tempVar69);
        StoragePage->BeforeDraw += [&] (o, e)
        {
            StorageGoldText->setText(Gold > 0 ? std::string::Format("{0:###,###,###}", Gold) : "0");
            if (MyRankId == 0)
            {
                StorageGoldRemove->setVisible(true);
            }
            else
            {
                StorageGoldRemove->setVisible(false);
            }
    
        };
        StorageGoldText = new MirLabel();
        StorageGoldText->setParent(StoragePage);
        Size tempVar70(125, 12);
        StorageGoldText->setSize(&tempVar70);
        Point tempVar71(194, 312);
        StorageGoldText->setLocation(&tempVar71);
        StorageGoldText->setVisible(true);
        StorageGoldText->setText("0");
        StorageGoldText->setNotControl(true);
        StorageGoldAdd = new MirButton();
        StorageGoldAdd->setParent(StoragePage);
        StorageGoldAdd->setLibrary(Libraries::Prguse);
        StorageGoldAdd->setIndex(918);
        StorageGoldAdd->setVisible(true);
        StorageGoldAdd->setEnabled(true);
        Point tempVar72(158, 313);
        StorageGoldAdd->setLocation(&tempVar72);
        StorageGoldAdd->Click += [&] (o, e)
        {
            StorageAddGold();
        };

        StorageGoldRemove = new MirButton();
        StorageGoldRemove->setParent(StoragePage);
        StorageGoldRemove->setLibrary(Libraries::Prguse);
        StorageGoldRemove->setIndex(917);
        StorageGoldRemove->setVisible(true);
        StorageGoldRemove->setEnabled(true);
        Point tempVar73(142, 313);
        StorageGoldRemove->setLocation(&tempVar73);
        StorageGoldRemove->Click += [&] (o, e)
        {
            StorageRemoveGold();
        };

        StorageGrid = std::vector<MirItemCell*>(8 * 14);
        {
            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 14; y++)
                {
                    int idx = 8 * y + x;
                    MirItemCell *tempVar74 = new MirItemCell();
                    tempVar74->setItemSlot(idx);
                    tempVar74->setGridType(MirGridType::GuildStorage);
                    tempVar74->setLibrary(Libraries::Items);
                    tempVar74->setParent(StoragePage);
                    Size tempVar75(35, 35);
                    tempVar74->setSize(&tempVar75);
                    Point tempVar76(x * 35 + 31 + x, y * 35 + 20 + y);
                    tempVar74->setLocation(&tempVar76);
                    StorageGrid[idx] = tempVar74;
                    if (y > 7)
                    {
                        StorageGrid[idx]->setVisible(false);
                    }
                    StorageGrid[idx]->MouseWheel += StoragePanel_MouseWheel;

                    delete tempVar74;
                }
            }
        }

        StorageUpButton = new MirButton();
        StorageUpButton->setHoverIndex(198);
        StorageUpButton->setIndex(197);
        StorageUpButton->setVisible(true);
        StorageUpButton->setLibrary(Libraries::Prguse2);
        Point tempVar77(337, 1);
        StorageUpButton->setLocation(&tempVar77);
        Size tempVar78(16, 14);
        StorageUpButton->setSize(&tempVar78);
        StorageUpButton->setParent(StoragePage);
        StorageUpButton->setPressedIndex(199);
        StorageUpButton->setSound(SoundList::ButtonA);
        StorageUpButton->Click += [&] (o, e)
        {
            if (StorageIndex == 0)
            {
                return;
            }
            StorageIndex--;
            UpdateStorage();
            StorageUpdatePositionBar();
        };

        StorageDownButton = new MirButton();
        StorageDownButton->setHoverIndex(208);
        StorageDownButton->setIndex(207);
        StorageDownButton->setVisible(true);
        StorageDownButton->setLibrary(Libraries::Prguse2);
        Point tempVar79(337, 318);
        StorageDownButton->setLocation(&tempVar79);
        Size tempVar80(16, 14);
        StorageDownButton->setSize(&tempVar80);
        StorageDownButton->setParent(StoragePage);
        StorageDownButton->setPressedIndex(209);
        StorageDownButton->setSound(SoundList::ButtonA);
        StorageDownButton->Click += [&] (o, e)
        {
            if (StorageIndex >= 6)
            {
                StorageIndex = 5;
            }
            StorageIndex++;
            UpdateStorage();
            StorageUpdatePositionBar();
        };

        StoragePositionBar = new MirButton();
        StoragePositionBar->setIndex(206);
        StoragePositionBar->setLibrary(Libraries::Prguse2);
        Point tempVar81(337, 16);
        StoragePositionBar->setLocation(&tempVar81);
        StoragePositionBar->setParent(StoragePage);
        StoragePositionBar->setMovable(true);
        StoragePositionBar->setVisible(true);
        StoragePositionBar->setSound(SoundList::None);
        StoragePositionBar->OnMoving->addListener("StoragePositionBar_OnMoving", std::bind(&GuildDialog::StoragePositionBar_OnMoving, this, std::placeholders::_1, std::placeholders::_2));

        StoragePage->MouseWheel += StoragePanel_MouseWheel;
        StoragePageBase->MouseWheel += StoragePanel_MouseWheel;

        #pragma endregion

        #pragma region RankDialogUI
        RankPage = new MirImageControl();
        RankPage->setParent(this);
        Size tempVar82(352, 372);
        RankPage->setSize(&tempVar82);
        Point tempVar83(0, 60);
        RankPage->setLocation(&tempVar83);
        RankPage->setVisible(false);
        RankPage->BeforeDraw += [&] (o, e)
        {
            RequestUpdateMembers();
        };
        RanksSelectTextL = new MirLabel();
        RanksSelectTextL->setText("Edit Rank");
        Point tempVar84(42, 18);
        RanksSelectTextL->setLocation(&tempVar84);
        Size tempVar85(150, 20);
        RanksSelectTextL->setSize(&tempVar85);
        RanksSelectTextL->setForeColour(Color::White);
        RanksSelectTextL->setParent(RankPage);
        RanksSelectTextL->setNotControl(true);
        RanksSelectTextR = new MirLabel();
        RanksSelectTextR->setText("Select Rank");
        Point tempVar86(198, 18);
        RanksSelectTextR->setLocation(&tempVar86);
        Size tempVar87(150, 20);
        RanksSelectTextR->setSize(&tempVar87);
        RanksSelectTextR->setForeColour(Color::White);
        RanksSelectTextR->setParent(RankPage);
        RanksSelectTextR->setNotControl(true);
        RanksSelectBox = new MirDropDownBox();
        RanksSelectBox->setParent(RankPage);
        Point tempVar88(198, 36);
        RanksSelectBox->setLocation(&tempVar88);
        Size tempVar89(130, 16);
        RanksSelectBox->setSize(&tempVar89);
        RanksSelectBox->setForeColour(Color::White);
        RanksSelectBox->setVisible(true);
        RanksSelectBox->setEnabled(true);
        RanksSelectBox->setBackColour(Color::FromArgb(255, 25, 25, 25));
        RanksSelectBox->setBorderColour(Color::FromArgb(255, 35, 35, 35));
        RanksSelectBox->ValueChanged->addListener([&] (o, e)
        {
            OnRankSelect(RanksSelectBox->_WantedIndex);
        });

        RanksName = new MirTextBox();
        Point tempVar90(42, 36);
        RanksName->setLocation(&tempVar90);
        Size tempVar91(130, 16);
        RanksName->setSize(&tempVar91);
        RanksName->setMaxLength(20);
        RanksName->setParent(RankPage);
        RanksName->setVisible(true);
        RanksName->setEnabled(false);
        RanksName->setText("");
        RanksName->setBorder(true);
        RanksName->setBackColour(Color::FromArgb(255, 25, 25, 25));
        RanksName->setBorderColour(Color::FromArgb(255, 35, 35, 35));
        RanksName->BeforeDraw += [&] (o, e)
        {
            RanksName_BeforeDraw();
        };
        RanksName->TextBox->KeyPress += RanksName_KeyPress;
        RanksSaveName = new MirButton();
        Point tempVar92(155, 290);
        RanksSaveName->setLocation(&tempVar92);
        RanksSaveName->setEnabled(false);
        RanksSaveName->setVisible(true);
        RanksSaveName->setParent(RankPage);
        RanksSaveName->setIndex(90);
        RanksSaveName->setHoverIndex(91);
        RanksSaveName->setPressedIndex(92);
        RanksSaveName->setLibrary(Libraries::Title);
        RanksSaveName->setSound(SoundList::ButtonA);
        RanksSaveName->Click += [&] (o, e)
        {
            RanksChangeName();
        };
        std::vector<std::string> Options = {"Edit ranks", "Recruit member", "Kick member", "Store item", "Retrieve item", "Alter alliance", "Change notice", "Activate Buff"};
        RanksOptionsButtons = std::vector<MirButton*>(8);
        RanksOptionsStatus = std::vector<MirImageControl*>(8);
        RanksOptionsTexts = std::vector<MirLabel*>(8);
        for (int i = 0; i < RanksOptionsButtons.size(); i++)
        {
            MirButton *tempVar93 = new MirButton();
            tempVar93->setVisible(true);
            tempVar93->setEnabled(false);
            tempVar93->setIndex(1346);
            tempVar93->setLibrary(Libraries::Prguse);
            tempVar93->setSound(SoundList::ButtonA);
            tempVar93->setParent(RankPage);
            Point tempVar94(i % 2 == 0 ? 42 : 202, i % 2 == 0 ? 120 + (i * 20) : 120 + ((i - 1) * 20));
            tempVar93->setLocation(&tempVar94);
            RanksOptionsButtons[i] = tempVar93;
            int index = i;
            RanksOptionsButtons[i]->Click += [&] (o, e)
            {
                SwitchRankOption(index);
            };

            delete tempVar93;
        }
        for (int i = 0; i < RanksOptionsStatus.size(); i++)
        {
            MirImageControl *tempVar95 = new MirImageControl();
            tempVar95->setVisible(false);
            tempVar95->setIndex(1347);
            tempVar95->setLibrary(Libraries::Prguse);
            tempVar95->setParent(RankPage);
            tempVar95->setNotControl(true);
            Point tempVar96(i % 2 == 0 ? 42 : 202, i % 2 == 0 ? 120 + (i * 20) : 120 + ((i - 1) * 20));
            tempVar95->setLocation(&tempVar96);
            RanksOptionsStatus[i] = tempVar95;
            int index = i;
            RanksOptionsStatus[i]->Click += [&] (o, e)
            {
                SwitchRankOption(index);
            };

            delete tempVar95;
        }
        for (int i = 0; i < RanksOptionsTexts.size(); i++)
        {
            MirLabel *tempVar97 = new MirLabel();
            tempVar97->setVisible(true);
            tempVar97->setNotControl(true);
            tempVar97->setParent(RankPage);
            Point tempVar98(17 + (i % 2 == 0 ? 42 : 202), i % 2 == 0 ? 118 + (i * 20) : 118 + ((i - 1) * 20));
            tempVar97->setLocation(&tempVar98);
            tempVar97->setAutoSize(true);
            tempVar97->setText(Options[i]);
            RanksOptionsTexts[i] = tempVar97;

            delete tempVar97;
        }

        #pragma endregion

        #pragma region BuffDialogUI

        BuffPage = new MirImageControl();
        BuffPage->setParent(this);
        Size tempVar99(352, 372);
        BuffPage->setSize(&tempVar99);
        Point tempVar100(360, 61);
        BuffPage->setLocation(&tempVar100);
        BuffPage->setIndex(1853);
        BuffPage->setLibrary(Libraries::Prguse);
        BuffPage->setVisible(false);
        BuffPage->MouseWheel += BuffsPanel_MouseWheel;


        Buffs = std::vector<GuildBuffButton*>(8);
        for (unsigned char i = 0; i < Buffs.size(); i++)
        {
            unsigned char Id = i;
            GuildBuffButton *tempVar101 = new GuildBuffButton();
            tempVar101->setParent(BuffPage);
            tempVar101->setVisible(true);
            Point tempVar102(4, 27 + (i * 38));
            tempVar101->setLocation(&tempVar102);
            tempVar101->Id = Id;
            Buffs[i] = tempVar101;
            Buffs[i]->Click += [&] (o, e)
            {
                RequestBuff(Id);
            };
            Buffs[i]->MouseWheel += BuffsPanel_MouseWheel;

            delete tempVar101;
        }

        PointsLeft = new MirLabel();
        PointsLeft->setDrawFormat(TextFormatFlags::HorizontalCenter);
        PointsLeft->setParent(BuffPage);
        Point tempVar103(118, 3);
        PointsLeft->setLocation(&tempVar103);
        Size tempVar104(100, 20);
        PointsLeft->setSize(&tempVar104);
        PointsLeft->setNotControl(true);

        UpButton = new MirButton();
        UpButton->setIndex(197);
        UpButton->setHoverIndex(198);
        UpButton->setPressedIndex(199);
        UpButton->setLibrary(Libraries::Prguse2);
        Point tempVar105(203, 24);
        UpButton->setLocation(&tempVar105);
        UpButton->setParent(BuffPage);
        UpButton->setSound(SoundList::ButtonA);
        UpButton->Click += [&] (o, e)
        {
            if (StartIndex == 0)
            {
                return;
            }
            StartIndex--;
            UpdatePositionBar();
            RefreshInterface();
        };
        DownButton = new MirButton();
        DownButton->setIndex(207);
        DownButton->setHoverIndex(208);
        DownButton->setPressedIndex(209);
        DownButton->setLibrary(Libraries::Prguse2);
        Point tempVar106(203, 317);
        DownButton->setLocation(&tempVar106);
        DownButton->setParent(BuffPage);
        DownButton->setSound(SoundList::ButtonA);
        DownButton->Click += [&] (o, e)
        {
            if (GuildBuffInfos.size() < 8)
            {
                return;
            }
            if (StartIndex == GuildBuffInfos.size() - 8)
            {
                return;
            }
            StartIndex++;
            UpdatePositionBar();
            RefreshInterface();
        };

        PositionBar = new MirButton();
        PositionBar->setIndex(205);
        PositionBar->setHoverIndex(206);
        PositionBar->setPressedIndex(206);
        PositionBar->setLibrary(Libraries::Prguse2);
        PositionBar->setParent(BuffPage);
        PositionBar->setMovable(true);
        PositionBar->setSound(SoundList::None);
        Point tempVar107(203, 39);
        PositionBar->setLocation(&tempVar107);
        PositionBar->OnMoving->addListener("PositionBar_OnMoving", std::bind(&GuildDialog::PositionBar_OnMoving, this, std::placeholders::_1, std::placeholders::_2));
        PositionBar->MouseUp->addListener([&] (o, e)
        {
            RefreshInterface();
        });

        #pragma endregion
    }

    void GuildDialog::RequestBuff(unsigned char Id)
    {
        if ((Id + StartIndex) > GuildBuffInfos.size())
        {
            return;
        }
        GuildBuffInfo *BuffInfo = GuildBuffInfos[Id + StartIndex];
        if (BuffInfo == nullptr)
        {
            return;
        }
        GuildBuff *Buff = FindGuildBuff(BuffInfo->Id);
        if (Buff == nullptr)
        {
            std::string Error = "";
            if (GameScene::Scene->GuildDialog->SparePoints < BuffInfo->PointsRequirement)
            {
                Error = "Insufficient points available.";
            }
            if (GameScene::Scene->GuildDialog->Level < BuffInfo->LevelRequirement)
            {
                Error = "Guild level too low.";
            }
            if (!GameScene::Scene->GuildDialog->GetMyOptions()::HasFlag(GuildRankOptions::CanActivateBuff))
            {
                Error = "Guild rank does not allow buff activation.";
            }
            if (Error != "")
            {
                MirMessageBox *messageBox = new MirMessageBox(Error);
                messageBox->Show();

                delete messageBox;
                return;
            }
            if (CMain::Time < LastRequest + 100)
            {
                return;
            }
            LastRequest = CMain::Time;
            C::GuildBuffUpdate *tempVar = new C::GuildBuffUpdate();
            tempVar->Action = 1;
            tempVar->Id = BuffInfo->Id;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            std::string Error = "";
            if (Buff->Active)
            {
                Error = "Buff is still active.";
            }
            if (GameScene::Scene->GuildDialog->Gold < BuffInfo->ActivationCost)
            {
                Error = "Insufficient guild funds.";
            }
            if (!GameScene::Scene->GuildDialog->GetMyOptions()::HasFlag(GuildRankOptions::CanActivateBuff))
            {
                Error = "Guild rank does not allow buff activation.";
            }
            if (Error != "")
            {
                MirMessageBox *messageBox = new MirMessageBox(Error);
                messageBox->Show();

                delete messageBox;
                return;
            }
            if (CMain::Time < LastRequest + 100)
            {
                return;
            }
            LastRequest = CMain::Time;
            C::GuildBuffUpdate *tempVar2 = new C::GuildBuffUpdate();
            tempVar2->Action = 2;
            tempVar2->Id = BuffInfo->Id;
            Network::Enqueue(tempVar2);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }
    }

    GuildBuff *GuildDialog::FindGuildBuff(int Index)
    {
        for (int i = 0; i < EnabledBuffs.size(); i++)
        {
            if ((EnabledBuffs[i] != nullptr) && (EnabledBuffs[i]->Id == Index))
            {
                return EnabledBuffs[i];
            }
        }
        return nullptr;
    }

    void GuildDialog::UpdatePositionBar()
    {
        int h = 277 - PositionBar->getSize()->Height;
        h = static_cast<int>((h / static_cast<float>(GuildBuffInfos.size() - 8)) * StartIndex);
        Point tempVar(203, 39 + h);
        PositionBar->setLocation(&tempVar);
    }

    void GuildDialog::PositionBar_OnMoving(std::any sender, MouseEventArgs *e)
    {
        constexpr int x = 203;
        int y = PositionBar->getLocation()->Y;
        if (y >= 296)
        {
            y = 296;
        }
        if (y < 39)
        {
            y = 39;
        }

        int h = 277 - PositionBar->getSize()->Height;
        h = static_cast<int>(BankersRounding::round(((y - 39) / (h / static_cast<float>(GuildBuffInfos.size() - 8)))));
        Point tempVar(x, y);
        PositionBar->setLocation(&tempVar);
        if (h == StartIndex)
        {
            return;
        }
        StartIndex = h;
        RefreshInterface();
    }

    void GuildDialog::BuffsPanel_MouseWheel(std::any sender, MouseEventArgs *e)
    {
        int count = e->Delta / SystemInformation::MouseWheelScrollDelta;

        if (StartIndex == 0 && count >= 0)
        {
            return;
        }
        if (StartIndex == (GuildBuffInfos.size() - 8) && count <= 0)
        {
            return;
        }
        StartIndex -= count > 0 ? 1 : -1;
        if (GuildBuffInfos.size() <= 8)
        {
            StartIndex = 0;
        }
        UpdatePositionBar();
        RefreshInterface();
    }

    void GuildDialog::RefreshInterface()
    {
        if (StartIndex < 0)
        {
            StartIndex = 0;
        }

        if (GuildBuffInfos.empty())
        {
            BuffButton->setVisible(false);
        }
        else
        {
            BuffButton->setVisible(true);
        }

        if (MapObject::User->GuildName == "")
        {
            Hide();
            return;
        }
        else
        {
            PointsLeft->setText(std::to_string(GameScene::Scene->GuildDialog->SparePoints));
            for (int i = 0; i < Buffs.size(); i++)
            {
                if ((StartIndex + i) > GuildBuffInfos.size() - 1)
                {
                    Buffs[i]->setVisible(false);
                    continue;
                }
                GuildBuffInfo *BuffInfo = GuildBuffInfos[i + StartIndex];
                if (BuffInfo == nullptr)
                {
                    Buffs[i]->setVisible(false);
                    continue;
                }
                Buffs[i]->setVisible(true);
                GuildBuff *Buff = FindGuildBuff(BuffInfo->Id);
                Buffs[i]->Name.setText(BuffInfo->name);
                Buffs[i]->Icon.setIndex(BuffInfo->Icon);

                if (Buff == nullptr)
                {
                    if (BuffInfo->LevelRequirement > GameScene::Scene->GuildDialog->Level)
                    {
                        Buffs[i]->Info.setText("Insufficient Level");
                        Buffs[i]->Info.setForeColour(Color::Red);
                        Buffs[i]->Icon.setIndex(Buffs[i]->Icon.getIndex() + 2);
                    }
                    else
                    {
                        Buffs[i]->Info.setText("Available");
                        Buffs[i]->Info.setForeColour(Buffs[i]->Name.getForeColour());
                        Buffs[i]->Icon.setIndex(Buffs[i]->Icon.getIndex() + 2);
                    }
                    Buffs[i]->Obtained.setText("");
                }
                else
                {
                    if (BuffInfo->TimeLimit > 0)
                    {
                        if (Buff->Active)
                        {
                            Buffs[i]->Info.setText("Counting down.");
                        }
                        else
                        {
                            Buffs[i]->Info.setText("Expired.");
                        }
                    }
                    else
                    {
                        Buffs[i]->Info.setText("Obtained.");
                    }
                    Buffs[i]->Info.setForeColour(Buffs[i]->Name.getForeColour());
                    if (Buff->Active)
                    {
                        Buffs[i]->Obtained.setText("Active");
                        Buffs[i]->Icon.setIndex(Buffs[i]->Icon.getIndex() + 1);
                    }
                    else
                    {
                        Buffs[i]->Obtained.setText("Inactive");
                    }
                }
            }
        }
    }

    GuildBuffInfo *GuildDialog::FindGuildBuffInfo(int Index)
    {
        if (!RequestedList)
        {
            RequestGuildBuffList();
        }
        for (int i = 0; i < GuildBuffInfos.size(); i++)
        {
            if (GuildBuffInfos[i]->Id == Index)
            {
                return GuildBuffInfos[i];
            }
        }
        return nullptr;
    }

    void GuildDialog::RequestGuildBuffList()
    {
        if (!RequestedList)
        {
            RequestedList = true;
            C::GuildBuffUpdate *tempVar = new C::GuildBuffUpdate();
            tempVar->Action = 0;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void GuildDialog::UpdateActiveStats()
    {
        std::string text = "";

        auto stats = new Stats();

        for (auto buff : EnabledBuffs)
        {
            if ((buff->Info == nullptr) || (!buff->Active))
            {
                continue;
            }

            stats->Add(buff->Info->Stats);
        }

        for (auto val : *stats)
        {
            auto c = val->second->Value < 0 ? "Decreases" : "Increases";

//C# TO C++ CONVERTER TODO TASK: There is no C++ equivalent to 'ToString':
            auto txt = std::string::Format("{0} {1} by: {2}{3}.\n", c, val->second.Key, val->second->Value, (val->second.Key->ToString().Contains("Percent") ? "%" : ""));

            text += txt;
        }

        ActiveStats = text;

//C# TO C++ CONVERTER TODO TASK: A 'delete stats' statement was not added since stats was passed to a method or constructor. Handle memory management manually.
    }

    void GuildDialog::CreateHintLabel(unsigned char Id)
    {
        GameScene::Scene->GuildBuffLabel = new MirControl();
        GameScene::Scene->GuildBuffLabel->setBackColour(Color::FromArgb(255, 50, 50, 50));
        GameScene::Scene->GuildBuffLabel->setBorder(true);
        GameScene::Scene->GuildBuffLabel->setBorderColour(Color::Gray);
        GameScene::Scene->GuildBuffLabel->setNotControl(true);
        GameScene::Scene->GuildBuffLabel->setParent(GameScene::Scene);
        GameScene::Scene->GuildBuffLabel->setOpacity(0.7F);
        GameScene::Scene->GuildBuffLabel->setDrawControlTexture(true);
        if (Id + StartIndex > GuildBuffInfos.size())
        {
            return;
        }

        GuildBuffInfo *Buff = GuildBuffInfos[Id + StartIndex];
        if (Buff == nullptr)
        {
            return;
        }
        MirLabel *HintName = new MirLabel();
        HintName->setAutoSize(true);
        HintName->setForeColour(Color::White);
        Point tempVar(4, 4);
        HintName->setLocation(&tempVar);
        HintName->setOutLine(true);
        HintName->setParent(GameScene::Scene->GuildBuffLabel);
        HintName->setText(Buff->name);

        Size tempVar2(std::max(GameScene::Scene->GuildBuffLabel->getSize()->Width, HintName->getDisplayRectangle()->Right + 4), std::max(GameScene::Scene->GuildBuffLabel->getSize()->Height, HintName->getDisplayRectangle()->Bottom));
        GameScene::Scene->GuildBuffLabel->setSize(&tempVar2);

        std::string ReqText = "";
        if (Buff->LevelRequirement > 0)
        {
            ReqText += "Minimum Guild Level: " + std::to_string(Buff->LevelRequirement);
        }
        if (Buff->PointsRequirement > 0)
        {
            if (ReqText != "")
            {
                ReqText += "\n";
            }
            ReqText += "Points Required: " + std::to_string(Buff->PointsRequirement);
        }
        if (Buff->ActivationCost > 0)
        {
            if (ReqText != "")
            {
                ReqText += "\n";
            }
            ReqText += "Activation Cost: " + std::to_string(Buff->ActivationCost) + " gold.";
            //if (ReqText != "") ReqText += "\n";
        }


        MirLabel *RequiredLabel = new MirLabel();
        RequiredLabel->setAutoSize(true);
        RequiredLabel->setForeColour(Color::White);
        Point tempVar3(4, GameScene::Scene->GuildBuffLabel->getDisplayRectangle()->Bottom);
        RequiredLabel->setLocation(&tempVar3);
        RequiredLabel->setOutLine(true);
        RequiredLabel->setParent(GameScene::Scene->GuildBuffLabel);
        RequiredLabel->setText(ReqText);
        Size tempVar4(std::max(GameScene::Scene->GuildBuffLabel->getSize()->Width, RequiredLabel->getDisplayRectangle()->Right + 4), std::max(GameScene::Scene->GuildBuffLabel->getSize()->Height, RequiredLabel->getDisplayRectangle()->Bottom));
        GameScene::Scene->GuildBuffLabel->setSize(&tempVar4);

        //code to dispay the buffs duration
        if (Buff->TimeLimit > 0)
        {
            GuildBuff *activeBuff = FindGuildBuff(Buff->Id);

            std::string text = "";

            if (activeBuff != nullptr && activeBuff->Active)
            {
                text = StringHelper::formatSimple("Time Remaining: {0} minutes", activeBuff->ActiveTimeRemaining);
            }
            else
            {
                text = StringHelper::formatSimple("Buff Length: {0} minutes.", std::to_string(Buff->TimeLimit));
            }
            MirLabel *TimeLabel = new MirLabel();
            TimeLabel->setAutoSize(true);
            TimeLabel->setForeColour(Color::White);
            Point tempVar5(4, GameScene::Scene->GuildBuffLabel->getDisplayRectangle()->Bottom);
            TimeLabel->setLocation(&tempVar5);
            TimeLabel->setOutLine(true);
            TimeLabel->setParent(GameScene::Scene->GuildBuffLabel);
            TimeLabel->setText(text);
            Size tempVar6(std::max(GameScene::Scene->GuildBuffLabel->getSize()->Width, TimeLabel->getDisplayRectangle()->Right + 4), std::max(GameScene::Scene->GuildBuffLabel->getSize()->Height, TimeLabel->getDisplayRectangle()->Bottom));
            GameScene::Scene->GuildBuffLabel->setSize(&tempVar6);

            delete TimeLabel;
        }


        //code to display the buff's effect
        MirLabel *InfoLabel = new MirLabel();
        InfoLabel->setAutoSize(true);
        InfoLabel->setForeColour(Color::White);
        Point tempVar7(4, GameScene::Scene->GuildBuffLabel->getDisplayRectangle()->Bottom);
        InfoLabel->setLocation(&tempVar7);
        InfoLabel->setOutLine(true);
        InfoLabel->setParent(GameScene::Scene->GuildBuffLabel);
        InfoLabel->setText(Buff->ShowStats());
        Size tempVar8(std::max(GameScene::Scene->GuildBuffLabel->getSize()->Width, InfoLabel->getDisplayRectangle()->Right + 4), std::max(GameScene::Scene->GuildBuffLabel->getSize()->Height, InfoLabel->getDisplayRectangle()->Bottom));
        GameScene::Scene->GuildBuffLabel->setSize(&tempVar8);

        delete InfoLabel;
        delete RequiredLabel;
        delete HintName;
    }

    void GuildDialog::ResetButtonStats()
    {
        if (MyOptions::HasFlag(GuildRankOptions::CanRetrieveItem) || MyOptions::HasFlag(GuildRankOptions::CanStoreItem))
        {
            StorageButton->setVisible(true);
        }
        else
        {
            StorageButton->setVisible(false);
        }

        if (MyOptions::HasFlag(GuildRankOptions::CanChangeRank))
        {
            RankButton->setVisible(true);
        }
        else
        {
            RankButton->setVisible(false);
        }

        if (MyOptions::HasFlag(GuildRankOptions::CanChangeNotice))
        {
            NoticeEditButton->setVisible(true);
        }
        else
        {
            NoticeEditButton->setVisible(false);
        }

        BuffButton->setVisible(true);
    }

    void GuildDialog::EditNotice()
    {
        if (Notice->getEnabled() == false)
        {
            Notice->setEnabled(true);
            Notice->SetFocus();
            NoticeEditButton->setVisible(false);
            NoticeSaveButton->setVisible(true);
        }
        else
        {
            Notice->setEnabled(false);
            NoticeEditButton->setIndex(560);
            NoticeEditButton->setVisible(true);
            NoticeSaveButton->setVisible(false);
            C::EditGuildNotice *tempVar = new C::EditGuildNotice();
            tempVar->notice = Notice->getMultiText().ToList();
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void GuildDialog::NoticeChange(std::vector<std::string> &newnotice)
    {
        NoticeEditButton->setIndex(560);
        Notice->setEnabled(false);
        NoticeScrollIndex = 0;
        Notice->setText("");
        Notice->setMultiText(newnotice.ToArray());
        NoticeChanged = false;
        UpdateNotice();
    }

    void GuildDialog::UpdateNotice(bool forward)
    {
        int NoticeScrollerIndex = NoticeScrollIndex;

        if (forward)
        {
            NoticeScrollerIndex += 24;
        }

        if (NoticeScrollerIndex >= Notice->getMultiText().size())
        {
            NoticeScrollerIndex = Notice->getMultiText().size() - 24;
        }
        if (NoticeScrollerIndex < 0)
        {
            NoticeScrollerIndex = 0;
        }
        if (Notice->getMultiText().size() != 0)
        {
            Notice->TextBox->SelectionLength = 1;
            Notice->TextBox->SelectionStart = Notice->TextBox->GetFirstCharIndexFromLine(NoticeScrollerIndex);
            Notice->TextBox->ScrollToCaret();
        }
    }

    void GuildDialog::UpdateNoticeScrollPosition()
    {
        int interval = 289 / (Notice->getMultiText().size() - 25);

        int x = 337;
        int y = 16 + (NoticeScrollIndex * interval);

        if (y >= NoticeDownButton->getLocation()->Y - 20)
        {
            y = NoticeDownButton->getLocation()->Y - 20;
        }
        if (y <= 16)
        {
            y = 16;
        }

        Point tempVar(x, y);
        NoticePositionBar->setLocation(&tempVar);
    }

    void GuildDialog::NoticePositionBar_OnMoving(std::any sender, MouseEventArgs *e)
    {
        int x = 337;
        int y = NoticePositionBar->getLocation()->Y;
        int TempIndex = 0;
        bool forward;

        if (y < 16)
        {
            y = 16;
        }

        int location = y - 16;
        int interval = 289 / (Notice->getMultiText().size() - 25);

        double yPoint = location / interval;

        TempIndex = static_cast<short>(std::floor(yPoint));

        if (NoticeScrollIndex < TempIndex)
        {
            forward = true;
            NoticeScrollIndex = static_cast<short>(std::floor(yPoint));
        }
        else
        {
            forward = false;
            NoticeScrollIndex = static_cast<short>(std::floor(yPoint));
        }

        if (NoticeScrollIndex >= Notice->getMultiText().size() - 25)
        {
            NoticeScrollIndex = Notice->getMultiText().size() - 25;
        }
        if (NoticeScrollIndex <= 0)
        {
            NoticeScrollIndex = 0;
        }

        UpdateNotice(forward);

        UpdateNoticeScrollPosition();
    }

    void GuildDialog::NoticePanel_MouseWheel(std::any sender, MouseEventArgs *e)
    {
        int count = e->Delta / SystemInformation::MouseWheelScrollDelta;

        if (NoticeScrollIndex == 0 && count >= 0)
        {
            return;
        }
        if (NoticeScrollIndex == Notice->getMultiText().size() - 25 && count <= 0)
        {
            return;
        }

        if (count > 0)
        {
            NoticeScrollIndex--;
            UpdateNotice();
        }
        else
        {
            NoticeScrollIndex++;
            UpdateNotice(true);
        }


        UpdateNoticeScrollPosition();
    }

    void GuildDialog::NoticePanel_KeyDown(std::any sender, KeyEventArgs *e)
    {
        switch (e->KeyCode)
        {
            case Keys->Up:
                if (NoticeScrollIndex == 0)
                {
                    break;
                }
                if (NoticeScrollIndex >= 25)
                {
                    NoticeScrollIndex -= 24;
                }
                NoticeScrollIndex--;
                break;
            case Keys->Home:
                if (NoticeScrollIndex == 0)
                {
                    break;
                }
                NoticeScrollIndex = 0;
                break;
            case Keys->Down:
                if (NoticeScrollIndex == Notice->getMultiText().size() - 1)
                {
                    break;
                }
                if (NoticeScrollIndex < 25)
                {
                    NoticeScrollIndex = 24;
                }
                NoticeScrollIndex++;
                break;
            case Keys->End:
                if (NoticeScrollIndex == Notice->getMultiText().size() - 1)
                {
                    break;
                }
                NoticeScrollIndex = Notice->getMultiText().size() - 1;
                break;
            case Keys->PageUp:
                if (NoticeScrollIndex == 0)
                {
                    break;
                }
                NoticeScrollIndex -= 25;
                break;
            case Keys->PageDown:
                if (NoticeScrollIndex == Notice->getMultiText().size() - 25)
                {
                    break;
                }
                NoticeScrollIndex += 25;
                break;
            default:
                return;
        }
        UpdateNotice();
        e->Handled = true;
    }

    void GuildDialog::NewMembersList(std::vector<GuildRank*> &NewRanks)
    {
        Ranks = NewRanks;
        MembersChanged = false;
        RefreshMemberList();
    }

    void GuildDialog::RefreshMemberList()
    {
        MemberScrollIndex = 0;
        std::vector<std::string> RankNames;
        for (int i = 0; i < Ranks.size(); i++)
        {
            if (Ranks[i] != nullptr)
            {
                int index = i;
                Ranks[i]->Index = index;
                RankNames.push_back(Ranks[i]->Name);
            }
            else
            {
                RankNames.push_back("Missing Rank");
            }
        }
        for (int i = 0; i < MembersRanks.size(); i++)
        {
            MembersRanks[i]->setItems(RankNames);
            MembersRanks[i]->MinimumOption = MyRankId;
        }
        RanksSelectBox->setItems(RankNames.ToList());
        RanksSelectBox->MinimumOption = 0;
        if (RankNames.size() < 255)
        {
            RanksSelectBox->getItems().push_back("Add New");
        }
        UpdateMembers();
        UpdateRanks();
    }

    void GuildDialog::MemberStatusChange(const std::string &name, bool online)
    {
        for (int i = 0; i < Ranks.size(); i++)
        {
            for (int j = 0; j < Ranks[i]->Members->size(); j++)
            {
                if (Ranks[i]->Members[j]->name == name)
                {
                    Ranks[i]->Members[j]->Online = online;
                }
            }
        }
        UpdateMembers();
    }

    void GuildDialog::OnNewRank(int Index, int SelectedIndex)
    {
        if (SelectedIndex >= Ranks.size())
        {
            return;
        }
        if (LastGuildMsg > CMain::Time)
        {
            return;
        }
        MirMessageBox *messageBox = new MirMessageBox(StringHelper::formatSimple("Are you sure you want to change the rank of {0} to {1}?", MembersName[Index]->getText(), Ranks[SelectedIndex]->Name), MirMessageBoxButtons::YesNo);

        messageBox->YesButton->Click += [&] (o, a)
        {
            C::EditGuildMember *tempVar = new C::EditGuildMember();
            tempVar->ChangeType = 2;
            tempVar->Name = MembersName[Index]->getText();
            tempVar->RankIndex = static_cast<unsigned char>(Ranks[SelectedIndex]->Index);
            tempVar->RankName = Ranks[SelectedIndex]->Name;
            Network::Enqueue(tempVar);
            LastGuildMsg = CMain::Time + 5000;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        };
        messageBox->Show();

        delete messageBox;
    }

    void GuildDialog::AddMember()
    {
        if (!MyOptions::HasFlag(GuildRankOptions::CanRecruit))
        {
            return;
        }
        if (LastGuildMsg > CMain::Time)
        {
            return;
        }
        C::EditGuildMember *tempVar = new C::EditGuildMember();
        tempVar->ChangeType = 0;
        tempVar->Name = MembersRecruitName->getText();
        Network::Enqueue(tempVar);
        LastGuildMsg = CMain::Time + 5000;
        MembersRecruitName->setText("");

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void GuildDialog::DeleteMember(int Index)
    {
        if (MembersName[Index]->getText() == MapControl::getUser()->Name)
        {
            return;
        }
        if (LastGuildMsg > CMain::Time)
        {
            return;
        }
        MirMessageBox *messageBox = new MirMessageBox(StringHelper::formatSimple("Are you sure you want to kick {0}?", MembersName[Index]->getText()), MirMessageBoxButtons::YesNo);

        messageBox->YesButton->Click += [&] (o, a)
        {
            C::EditGuildMember *tempVar = new C::EditGuildMember();
            tempVar->ChangeType = 1;
            tempVar->Name = MembersName[Index]->getText();
            Network::Enqueue(tempVar);
            LastGuildMsg = CMain::Time + 5000;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        };
        messageBox->Show();

        delete messageBox;
    }

    void GuildDialog::UpdateMembers()
    {
        MembersShowCount = 0;
        for (int i = 0; i < Ranks.size(); i++)
        {
            for (int j = 0; j < Ranks[i]->Members->size(); j++)
            {
                if (MembersShowOfflinesetting || Ranks[i]->Members[j].Online)
                {
                    MembersShowCount++;
                }
            }
        }
        if (MembersShowCount < MemberPageRows)
        {
            MemberScrollIndex = 0;
        }


        for (int i = 0; i < MembersRanks.size(); i++)
        {
            if (MembersShowCount > i)
            {
                MembersRanks[i]->setVisible(true);
            }
            else
            {
                MembersRanks[i]->setVisible(false);
            }
        }
        for (int i = 0; i < MembersName.size(); i++)
        {
            if (MembersShowCount > i)
            {
                MembersName[i]->setVisible(true);
            }
            else
            {
                MembersName[i]->setVisible(false);
            }
        }
        for (int i = 0; i < MembersStatus.size(); i++)
        {
            if (MembersShowCount > i)
            {
                MembersStatus[i]->setVisible(true);
            }
            else
            {
                MembersStatus[i]->setVisible(false);
            }
        }
        for (int i = 0; i < MembersDelete.size(); i++)
        {
            MembersDelete[i]->setVisible(false);
        }
        if (MyOptions::HasFlag(GuildRankOptions::CanRecruit))
        {
            RecruitMemberButton->setVisible(true);
            MembersRecruitName->setVisible(true);
            RecruitMemberLabel->setVisible(true);
        }
        else
        {
            RecruitMemberButton->setVisible(false);
            MembersRecruitName->setVisible(false);
            RecruitMemberLabel->setVisible(false);
        }

        int Offset = 0;
        int RowCount = 0;
        DateTime now = DateTime::Now;
        for (int i = 0; i < Ranks.size(); i++)
        {
            for (int j = 0; j < Ranks[i]->Members->size(); j++)
            {
                if (Offset < MemberScrollIndex)
                {
                    if ((MembersShowOfflinesetting) || (Ranks[i]->Members[j].Online))
                    {
                        Offset++;
                    }
                }
                else
                {

                    if ((!MembersShowOfflinesetting) && (Ranks[i]->Members[j]->Online == false))
                    {
                        continue;
                    }
                    if ((MyOptions::HasFlag(GuildRankOptions::CanChangeRank)) && (Ranks[i]->Index >= MyRankId))
                    {
                        MembersRanks[RowCount]->setEnabled(true);
                    }
                    else
                    {
                        MembersRanks[RowCount]->setEnabled(false);
                    }
                    if ((MyOptions::HasFlag(GuildRankOptions::CanKick)) && (Ranks[i]->Index >= MyRankId) && (Ranks[i]->Members[j].name != MapControl::getUser()->Name))
                    {
                        MembersDelete[RowCount]->setVisible(true);
                    }
                    else
                    {
                        MembersDelete[RowCount]->setVisible(false);
                    }
                    MembersRanks[RowCount]->setSelectedIndex(Ranks[i]->Index);
                    MembersName[RowCount]->setText(Ranks[i]->Members[j].name);
                    if (Ranks[i]->Members[j].Online)
                    {
                        MembersStatus[RowCount]->setForeColour(Color::LimeGreen);
                    }
                    else
                    {
                        MembersStatus[RowCount]->setForeColour(Color::White);
                    }
                    TimeSpan Diff = now - Ranks[i]->Members[j].LastLogin.ToLocalTime();
                    std::string text;
                    if (Ranks[i]->Members[j].Online)
                    {
                        text = "Online";
                    }
                    else
                    {
                        switch (Diff.Days)
                        {
                            case 0:
                                text = "Today";
                                break;
                            case 1:
                                text = "Yesterday";
                                break;
                            default:
                                text = std::to_string(Diff.Days) + "Days ago";
                                break;
                        }
                    }
                    MembersStatus[RowCount]->setText(text);
                    RowCount++;
                    if (RowCount > MemberPageRows - 1)
                    {
                        return;
                    }
                }
            }
        }
    }

    void GuildDialog::MembersShowOfflineSwitch()
    {
        if (MembersShowOfflinesetting)
        {
            MembersShowOfflinesetting = false;
            MembersShowOfflineStatus->setVisible(false);
        }
        else
        {
            MembersShowOfflinesetting = true;
            MembersShowOfflineStatus->setVisible(true);
        }
        UpdateMembers();
    }

    void GuildDialog::MembersPositionBar_OnMoving(std::any sender, MouseEventArgs *e)
    {
        int x = 337;
        int y = MembersPositionBar->getLocation()->Y;

        if (y >= MembersDownButton->getLocation()->Y - 20)
        {
            y = MembersDownButton->getLocation()->Y - 20;
        }
        if (y < 16)
        {
            y = 16;
        }

        int location = y - 16;
        int interval = 289 / (MembersShowCount - MemberPageRows);

        double yPoint = location / interval;

        MemberScrollIndex = static_cast<short>(std::floor(yPoint));
        if (MemberScrollIndex > MembersShowCount - MemberPageRows)
        {
            MemberScrollIndex = MembersShowCount - MemberPageRows;
        }
        if (MemberScrollIndex <= 0)
        {
            MemberScrollIndex = 0;
        }

        UpdateMembers();

        Point tempVar(x, y);
        MembersPositionBar->setLocation(&tempVar);
    }

    void GuildDialog::UpdateMembersScrollPosition()
    {
        int interval = 289 / (MembersShowCount - MemberPageRows);

        int x = 337;
        int y = 16 + (MemberScrollIndex * interval);

        if (y >= MembersDownButton->getLocation()->Y - 20)
        {
            y = MembersDownButton->getLocation()->Y - 20;
        }
        if (y <= 16)
        {
            y = 16;
        }

        Point tempVar(x, y);
        MembersPositionBar->setLocation(&tempVar);
    }

    void GuildDialog::MembersPanel_MouseWheel(std::any sender, MouseEventArgs *e)
    {
        int count = e->Delta / SystemInformation::MouseWheelScrollDelta;

        if (MemberScrollIndex == 0 && count >= 0)
        {
            return;
        }
        if (MemberScrollIndex == MembersShowCount - MemberPageRows && count <= 0)
        {
            return;
        }
        MemberScrollIndex -= count > 0 ? 1 : -1;
        UpdateMembers();
        UpdateMembersScrollPosition();
    }

    void GuildDialog::MembersPanel_KeyDown(std::any sender, KeyEventArgs *e)
    {
        switch (e->KeyCode)
        {
            case Keys->Up:
                if (MemberScrollIndex == 0)
                {
                    break;
                }
                MemberScrollIndex--;
                break;
            case Keys->Home:
                if (MemberScrollIndex == 0)
                {
                    break;
                }
                MemberScrollIndex = 0;
                break;
            case Keys->Down:
                if (MembersShowCount < MemberPageRows)
                {
                    break;
                }
                if (MemberScrollIndex == MembersShowCount - MemberPageRows)
                {
                    break;
                }
                MemberScrollIndex++;
                break;
            case Keys->End:
                if (MembersShowCount < MemberPageRows)
                {
                    break;
                }
                if (MemberScrollIndex == MembersShowCount - MemberPageRows)
                {
                    break;
                }
                MemberScrollIndex = MembersShowCount - MemberPageRows;
                break;
            case Keys->PageUp:
                if (MemberScrollIndex == 0)
                {
                    break;
                }
                MemberScrollIndex -= 25;
                if (MemberScrollIndex < 0)
                {
                    MemberScrollIndex = 0;
                }
                break;
            case Keys->PageDown:
                if (MembersShowCount < MemberPageRows)
                {
                    break;
                }
                if (MemberScrollIndex == MembersShowCount - 25)
                {
                    break;
                }
                MemberScrollIndex += 25;
                if (MemberScrollIndex > (MembersShowCount - MemberPageRows))
                {
                    MemberScrollIndex = MembersShowCount - MemberPageRows;
                }
                break;
            default:
                return;
        }
        UpdateMembers();
        UpdateMembersScrollPosition();
        e->Handled = true;
    }

    void GuildDialog::StatusExpBar_BeforeDraw(std::any sender, EventArgs *e)
    {
        if (GameScene::Scene->GuildDialog->MaxExperience == 0)
        {
            StatusExpLabel->setText("");
            return;
        }
        if (StatusExpBar->getLibrary() == nullptr)
        {
            return;
        }

        Size tempVar(260, 22);
        StatusExpBar->getLibrary()->Draw(424, StatusExpBar->getDisplayLocation(), &tempVar, Color::White);

        double percent = GameScene::Scene->GuildDialog->Experience / static_cast<double>(GameScene::Scene->GuildDialog->MaxExperience);
        StatusExpLabel->setText(std::string::Format("{0:#0.##%}", percent));
        if (percent > 1)
        {
            percent = 1;
        }
        if (percent <= 0)
        {
            return;
        }
        Rectangle *section = new Rectangle();
        section->Size = new Size(static_cast<int>((260 - 3) * percent), 22);

        StatusExpBar->getLibrary()->Draw(StatusExpBar->getIndex(), section, StatusExpBar->getDisplayLocation(), Color::White, false);

//C# TO C++ CONVERTER TODO TASK: A 'delete section' statement was not added since section was passed to a method or constructor. Handle memory management manually.
    }

    void GuildDialog::NewRankRecieved(GuildRank *New)
    {
        int NewIndex = Ranks.size() > 1 ? Ranks.size() - 1 : 1;
        Ranks.Insert(NewIndex, New);
        Ranks[Ranks.size() - 1]->Index = Ranks.size() - 1;
        RefreshMemberList();
        UpdateRanks();
    }

    void GuildDialog::MyRankChanged(GuildRank *New)
    {
        MyOptions = New->Options;

        MapObject::User->GuildRankName = New->Name;
        GuildMember *Member = nullptr;
        int OldRank = MyRankId;
        MyRankId = New->Index;
        if (OldRank >= Ranks.size())
        {
            return;
        }
        for (int i = 0; i < Ranks[OldRank]->Members->size(); i++)
        {
            if (Ranks[OldRank]->Members[i]->name == MapObject::User->Name)
            {
                Member = Ranks[OldRank]->Members[i];
                Ranks[OldRank]->Members->Remove(Member);
                break;
            }
        }

        if (Member == nullptr)
        {
            return;
        }
        if (Ranks.size() <= New->Index)
        {
            Ranks[MyRankId]->Members->push_back(Member);
            MembersChanged = true;
            return;
        }
        Ranks[New->Index]->Members->push_back(Member);

        ResetButtonStats();
        UpdateMembers();

    }

    void GuildDialog::RankChangeRecieved(GuildRank *New)
    {
        for (int i = 0; i < Ranks.size(); i++)
        {
            if (Ranks[i]->Index == New->Index)
            {
                if (Ranks[i]->Name == MapObject::User->GuildRankName)
                {
                    for (int j = 0; j < Ranks[i]->Members->size(); j++)
                    {
                        if (Ranks[i]->Members[j]->name == MapObject::User->Name)
                        {
                            MapObject::User->GuildRankName = New->Name;
                            MyOptions = New->Options;
                            ResetButtonStats();
                            UpdateMembers();
                        }
                    }
                }
                if (Ranks[i]->Name == New->Name)
                {
                    Ranks[i] = New;
                }
                else
                {
                    Ranks[i] = New;
                    RefreshMemberList();
                }
            }
        }
        UpdateRanks();
    }

    void GuildDialog::UpdateRanks()
    {
        if ((RanksSelectBox->getSelectedIndex() == -1) || (Ranks[RanksSelectBox->getSelectedIndex()]->Index < MyRankId))
        {
            for (int i = 0; i < RanksOptionsButtons.size(); i++)
            {
                RanksOptionsButtons[i]->setEnabled(false);
            }
            for (int i = 0; i < RanksOptionsStatus.size(); i++)
            {
                RanksOptionsStatus[i]->setEnabled(false);
            }
            if (RanksSelectBox->getSelectedIndex() == -1)
            {
                for (int i = 0; i < RanksOptionsStatus.size(); i++)
                {
                    RanksOptionsStatus[i]->setVisible(false);
                }
            }
            LastRankNameChange = std::numeric_limits<long long>::max();
            RanksName->setText("");
            return;
        }
        RanksName->setText(Ranks[RanksSelectBox->getSelectedIndex()]->Name);
        if (Ranks[RanksSelectBox->getSelectedIndex()]->Index >= MyRankId)
        {
            LastRankNameChange = 0;
        }
        else
        {
            LastRankNameChange = std::numeric_limits<long long>::max();
        }
        for (int i = 0; i < RanksOptionsStatus.size(); i++)
        {
            if (Ranks[RanksSelectBox->getSelectedIndex()]->Options.HasFlag(static_cast<GuildRankOptions>(1 << i)))
            {
                RanksOptionsStatus[i]->setVisible(true);
            }
            else
            {
                RanksOptionsStatus[i]->setVisible(false);
            }
            if (Ranks[RanksSelectBox->getSelectedIndex()]->Index >= MyRankId)
            {
                RanksOptionsButtons[i]->setEnabled(true);
            }
            else
            {
                RanksOptionsButtons[i]->setEnabled(false);
            }
        }
    }

    void GuildDialog::OnRankSelect(int Index)
    {
        if (Index < Ranks.size())
        {
            RanksSelectBox->setSelectedIndex(Index);
        }
        else
        {
            if (Ranks.size() == 255)
            {
                return;
            }
            if (LastGuildMsg > CMain::Time)
            {
                return;
            }
            MirMessageBox *messageBox = new MirMessageBox("Are you sure you want to create a new rank?", MirMessageBoxButtons::YesNo);
            messageBox->YesButton->Click += [&] (o, a)
            {
                C::EditGuildMember *tempVar = new C::EditGuildMember();
                tempVar->ChangeType = 4;
                tempVar->RankName = StringHelper::formatSimple("Rank-{0}", Ranks.size() - 1);
                Network::Enqueue(tempVar);
                LastGuildMsg = CMain::Time + 5000;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            };
            messageBox->Show();

            delete messageBox;
        }
        UpdateRanks();
    }

    void GuildDialog::SwitchRankOption(int OptionIndex)
    {
        if ((RanksSelectBox->getSelectedIndex() == -1) || (RanksSelectBox->getSelectedIndex() >= Ranks.size()))
        {
            return;
        }
        if (LastGuildMsg > CMain::Time)
        {
            return;
        }
        C::EditGuildMember *tempVar = new C::EditGuildMember();
        tempVar->ChangeType = 5;
        tempVar->RankIndex = static_cast<unsigned char>(Ranks[RanksSelectBox->getSelectedIndex()]->Index);
        tempVar->RankName = std::to_string(OptionIndex);
        tempVar->Name = RanksOptionsStatus[OptionIndex]->getVisible() ? "false" : "true";
        Network::Enqueue(tempVar);
        LastGuildMsg = CMain::Time + 300;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void GuildDialog::RanksChangeName()
    {
        if (!RanksName->getText().empty())
        {
            C::EditGuildMember *tempVar = new C::EditGuildMember();
            tempVar->ChangeType = 3;
            tempVar->RankIndex = static_cast<unsigned char>(Ranks[RanksSelectBox->getSelectedIndex()]->Index);
            tempVar->RankName = RanksName->getText();
            Network::Enqueue(tempVar);
            LastRankNameChange = CMain::Time + 5000;
            RanksName->setEnabled(false);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void GuildDialog::RanksName_KeyPress(std::any sender, KeyPressEventArgs *e)
    {
        switch (e->KeyChar)
        {
            case static_cast<char>('\\'):
                e->Handled = true;
                break;
            case static_cast<char>(Keys->Enter):
                e->Handled = true;
                RanksChangeName();
                break;
            case static_cast<char>(Keys->Escape):
                e->Handled = true;
                UpdateRanks();
                break;
        }
    }

    void GuildDialog::RanksName_BeforeDraw()
    {
        if (LastRankNameChange < CMain::Time)
        {
            RanksName->setEnabled(true);
            RanksSaveName->setEnabled(true);
        }
        else
        {
            RanksName->setEnabled(false);
            RanksSaveName->setEnabled(false);
        }
    }

    void GuildDialog::StoragePositionBar_OnMoving(std::any sender, MouseEventArgs *e)
    {
        int x = 337;
        int y = StoragePositionBar->getLocation()->Y;

        if (y >= StorageDownButton->getLocation()->Y - 20)
        {
            y = StorageDownButton->getLocation()->Y - 20;
        }
        if (y < 16)
        {
            y = 16;
        }

        int location = y - 16;
        int interval = 289 / 8;

        double yPoint = location / interval;

        StorageIndex = static_cast<short>(std::floor(yPoint));
        if (StorageIndex > 6)
        {
            StorageIndex = 6;
        }
        if (StorageIndex <= 0)
        {
            StorageIndex = 0;
        }
        UpdateStorage();

        Point tempVar(x, y);
        StoragePositionBar->setLocation(&tempVar);
    }

    void GuildDialog::StoragePanel_MouseWheel(std::any sender, MouseEventArgs *e)
    {
        int count = e->Delta / SystemInformation::MouseWheelScrollDelta;

        if (StorageIndex == 0 && count >= 0)
        {
            return;
        }
        if (StorageIndex == 6 && count <= 0)
        {
            return;
        }

        StorageIndex -= count;

        if (StorageIndex < 0)
        {
            StorageIndex = 0;
        }
        if (StorageIndex > 6)
        {
            StorageIndex = 6;
        }

        UpdateStorage();

        StorageUpdatePositionBar();
    }

    void GuildDialog::StorageUpdatePositionBar()
    {

        int interval = 289 / 6;

        int x = 337;
        int y = 16 + (StorageIndex * interval);

        if (y >= StorageDownButton->getLocation()->Y - 20)
        {
            y = StorageDownButton->getLocation()->Y - 20;
        }
        if (y <= 16)
        {
            y = 16;
        }

        Point tempVar(x, y);
        StoragePositionBar->setLocation(&tempVar);
    }

    void GuildDialog::UpdateStorage()
    {
        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 14; y++)
            {
                int idx = 8 * y + x;
                if (StorageIndex <= y && (y < StorageIndex + 8))
                {
                    Point tempVar(x * 35 + 31 + x, (y - StorageIndex) * 35 + 20 + (y - StorageIndex));
                    StorageGrid[idx]->setLocation(&tempVar);
                    StorageGrid[idx]->setVisible(true);
                }
                else
                {
                    StorageGrid[idx]->setVisible(false);
                }
            }
        }
    }

    void GuildDialog::StorageAddGold()
    {
        if (LastGuildMsg > CMain::Time)
        {
            return;
        }
        MirAmountBox *amountBox = new MirAmountBox("Deposit", 116, GameScene::Gold);

        amountBox->OKButton->Click += [&] (o, a)
        {
            if (amountBox->Amount <= 0)
            {
                return;
            }
            LastGuildMsg = CMain::Time + 100;
            C::GuildStorageGoldChange *tempVar = new C::GuildStorageGoldChange();
            tempVar->Type = 0;
            tempVar->Amount = amountBox->Amount;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        };

        amountBox->Show();

        delete amountBox;
    }

    void GuildDialog::StorageRemoveGold()
    {
        if (LastGuildMsg > CMain::Time)
        {
            return;
        }
        MirAmountBox *amountBox = new MirAmountBox("Gold to retrieve:", 116, Gold);

        amountBox->OKButton->Click += [&] (o, a)
        {
            if (amountBox->Amount <= 0)
            {
                return;
            }
            LastGuildMsg = CMain::Time + 100;
            C::GuildStorageGoldChange *tempVar = new C::GuildStorageGoldChange();
            tempVar->Type = 1;
            tempVar->Amount = amountBox->Amount;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        };

        amountBox->Show();

        delete amountBox;
    }

    void GuildDialog::RequestUpdateNotice()
    {
        if ((NoticeChanged) && (LastNoticeRequest < CMain::Time))
        {
            LastNoticeRequest = CMain::Time + 5000;
            C::RequestGuildInfo *tempVar = new C::RequestGuildInfo();
            tempVar->Type = 0;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void GuildDialog::RequestUpdateMembers()
    {
        if ((MembersChanged) && (LastMemberRequest < CMain::Time))
        {
            LastMemberRequest = CMain::Time + 5000;
            C::RequestGuildInfo *tempVar = new C::RequestGuildInfo();
            tempVar->Type = 1;
            Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void GuildDialog::RightDialog(unsigned char Rpageid)
    {
        StatusPage->setVisible(false);
        BuffPage->setVisible(false);

        StatusButton->setIndex(103);
        BuffButton->setIndex(95);

        switch (Rpageid)
        {
            case 0:
                StatusPage->setVisible(true);
                StatusButton->setIndex(104);
                break;
            case 1:
                BuffPage->setVisible(true);
                BuffButton->setIndex(96);
                break;
        }
    }

    void GuildDialog::LeftDialog(unsigned char Lpageid)
    {
        NoticePage->setVisible(false);
        MembersPage->setVisible(false);
        StoragePage->setVisible(false);
        RankPage->setVisible(false);

        NoticeButton->setIndex(93);
        MembersButton->setIndex(99);
        StorageButton->setIndex(105);
        RankButton->setIndex(101);

        switch (Lpageid)
        {
            case 0:
                NoticePage->setVisible(true);
                NoticeButton->setIndex(94);
                RequestUpdateNotice();
                break;
            case 1:
                MembersPage->setVisible(true);
                MembersButton->setIndex(100);
                RequestUpdateMembers();
                break;
            case 2:
                StoragePage->setVisible(true);
                StorageButton->setIndex(106);
                C::GuildStorageItemChange *tempVar = new C::GuildStorageItemChange();
                tempVar->Type = 3;
                Network::Enqueue(tempVar);
                break;
            case 3:
                RankPage->setVisible(true);
                RankButton->setIndex(102);
                RequestUpdateMembers();
                break;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void GuildDialog::StatusChanged(GuildRankOptions status)
    {
        Notice->setEnabled(false);
        NoticeEditButton->setIndex(85);
        MyOptions = status;

        if (MyOptions::HasFlag(GuildRankOptions::CanChangeNotice))
        {
            NoticeEditButton->setVisible(true);
        }
        else
        {
            NoticeEditButton->setVisible(false);
        }

        if (MyOptions::HasFlag(GuildRankOptions::CanChangeRank))
        {
            RankButton->setVisible(true);
        }
        else
        {
            RankButton->setVisible(false);
        }

        if ((MyOptions::HasFlag(GuildRankOptions::CanStoreItem)) || (MyOptions::HasFlag(GuildRankOptions::CanRetrieveItem)))
        {
            StorageButton->setVisible(true);
        }
        else
        {
            StorageButton->setVisible(false);
        }

        if (GuildBuffInfos.empty())
        {
            BuffButton->setVisible(false);
        }
        else
        {
            BuffButton->setVisible(true);
        }

    }

    void GuildDialog::Show()
    {
        if (getVisible())
        {
            return;
        }

        if (MapControl::getUser()->GuildName == "")
        {
            MirMessageBox *messageBox = new MirMessageBox(GameLanguage::NotInGuild, MirMessageBoxButtons::OK);
            messageBox->Show();

            delete messageBox;
            return;
        }
        setVisible(true);

        if (NoticePage->getVisible())
        {
            if ((NoticeChanged) && (LastNoticeRequest < CMain::Time))
            {
                LastNoticeRequest = CMain::Time + 5000;
                C::RequestGuildInfo *tempVar = new C::RequestGuildInfo();
                tempVar->Type = 0;
                Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            NoticeButton->setIndex(94);
        }
        if (StatusPage->getVisible())
        {
            StatusButton->setIndex(104);
        }


    }

    GuildBuffButton::GuildBuffButton()
    {
        setBorderColour(Color::Orange);

        Size tempVar(188, 33);
        setSize(&tempVar);
        Icon = new MirImageControl();
        Icon->setIndex(0);
        Icon->setLibrary(Libraries::GuildSkill);
        Icon->setParent(this);
        Point tempVar2(1, 0);
        Icon->setLocation(&tempVar2);
        Icon->setNotControl(true);
        Name = new MirLabel();
        Name->setAutoSize(true);
        Name->setParent(this);
        Point tempVar3(35, 1);
        Name->setLocation(&tempVar3);
        Name->setNotControl(true);
        Info = new MirLabel();
        Info->setAutoSize(true);
        Info->setParent(this);
        Point tempVar4(35, 17);
        Info->setLocation(&tempVar4);
        Info->setNotControl(true);
        Obtained = new MirLabel();
        Obtained->setDrawFormat(TextFormatFlags::Right);
        Obtained->setAutoSize(true);
        Obtained->setParent(this);
        Point tempVar5(140, 17);
        Obtained->setLocation(&tempVar5);
        Obtained->setNotControl(true);
        Obtained->setText("");
    }

    void GuildBuffButton::OnMouseEnter()
    {
        MirControl::OnMouseEnter();
        GameScene::Scene->GuildDialog->CreateHintLabel(Id);
        setBorder(true);
    }

    void GuildBuffButton::OnMouseLeave()
    {
        MirControl::OnMouseLeave();
        GameScene::Scene->DisposeGuildBuffLabel();
        setBorder(false);
    }
}
